<div class="layui-fluid layui-anim febs-anim" id="febs-oilPrice" lay-title="油品价格管理">
    <div class="layui-row febs-container">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body febs-table-full">
                    <form class="layui-form layui-table-form" lay-filter="oilPrice-table-form">
                        <div class="layui-row">
                            <div class="layui-col-md9">
                                <div class="layui-form-item">
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">变价日期</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="fromDate" autocomplete="off" lay-verify="date"
	                                       id="fromDate" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">变价油站</label>
                                        <div class="layui-input-inline">
                                            <select name="siteCode1"
							                        lay-verify="required"
							                        xm-select-direction="down"
							                        xm-select="oilPrice-search-site"
							                        xm-select-skin="default"
							                        xm-select-show-count="1"
							                        xm-select-height="36px"
													>
							                </select>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">OA审核</label>
                                        <div class="layui-input-inline">
                                            <input type="checkbox"  name="oaChecked1" lay-filter="febs-form-group-switch" lay-skin="switch" lay-text="是|否" value="0">
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label layui-form-label-sm">Envory发布</label>
                                        <div class="layui-input-inline">
                                            <input type="checkbox" name="envoryUpdate1" lay-filter="febs-form-group-switch1" lay-skin="switch" lay-text="是|否" value="0">
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-col-md3 layui-col-sm12 layui-col-xs12 table-action-area">
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="pricequery">
                                    <i class="layui-icon">&#xe848;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="pricereset">
                                    <i class="layui-icon">&#xe79b;</i>
                                </div>
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="priceadd">
                                    <i class="layui-icon">&#xe624;</i>
                                </div>

                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="pricedelete">
                                    <i class="layui-icon">&#xe640;</i>
                                </div>
                                
                                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="priceexport">
                                    <i class="layui-icon">&#xe67d;</i>
                                </div>
                            </div>
                        </div>
                    </form>
                    <table lay-filter="oilPriceTable" lay-data="{id: 'oilPriceTable'}"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="oaCheck">
    {{#
    var oaCheck = {
    1: {title: '已审批', color: 'green'},
    0: {title: '未审批', color: 'volcano'}
    }[d.oaChecked];
    }}
    <span class="layui-badge febs-tag-{{oaCheck.color}}">{{ oaCheck.title }}</span>
</script>

<script type="text/html" id="envoryUpdate">
    {{#
    var envoryUpdate = {
    1: {title: '已发布', color: 'green'},
    0: {title: '未发布', color: 'volcano'}
    }[d.envoryUpdate];
    }}
    <span class="layui-badge febs-tag-{{envoryUpdate.color}}">{{ envoryUpdate.title }}</span>
</script>
<script type="text/html" id="oilPrice-option">
    <span shiro:lacksPermission="shell:view,shell:update,shell:delete">
        <span class="layui-badge-dot febs-bg-orange"></span> 无权限
    </span>
	<span shiro:hasPermission="shell:update"><button lay-event="update" type="button" class="layui-btn layui-btn-xs">修改</button></span>
	<span shiro:hasPermission="shell:delete"><button lay-event="delete" type="button" class="layui-btn layui-btn-xs">删除</button></span>
</script>
<script data-th-inline="none" type="text/javascript">
    layui.use(['dropdown', 'jquery', 'laydate', 'formSelects', 'form', 'table', 'febs', 'treeSelect'], function () {
        var $ = layui.jquery,
            laydate = layui.laydate,
            febs = layui.febs,
            form = layui.form,
            table = layui.table,
            formSelects = layui.formSelects,
            treeSelect = layui.treeSelect,
            dropdown = layui.dropdown,
            $view = $('#febs-oilPrice'),
            $query = $view.find('#pricequery'),
            $reset = $view.find('#pricereset'),
            $add = $view.find('#priceadd'),
            $update = $view.find('#priceupdate'),
            $delete = $view.find('#pricedelete'),
            $export = $view.find('#priceexport'),
            $searchForm = $view.find('form'),
            sortObject = {field: 'fromDate', type: null},
            tableIns;

        form.render();

        initTable();
        
        formSelects.render();

        laydate.render({
            elem: '#fromDate',
            range: true,
            trigger: 'click'
        });

        formSelects.config('oilPrice-search-site', {
            searchUrl: ctx + 'dictsite/station',
            response: {
                statusCode: 200
            },
            beforeSuccess: function (id, url, searchVal, result) {
                var data = result.data;
                var tranData = [];
                for (var i = 0; i < data.length; i++) {
                    tranData.push({
                        name: data[i].stationName,
                        value: data[i].stationCode
                    })
                }
                result.data = tranData;
                return result;
            },
            error: function (id, url, searchVal, err) {
                console.error(err);
                febs.alert.error('获取油站列表失败');
            }
        });
        
        form.on('switch(febs-form-group-switch)', function (data) {
        	if(data.elem.checked) {
        		$searchForm.find('input[name="oaChecked1"]').val(1);
        	}else{
        		$searchForm.find('input[name="oaChecked1"]').val(0);
        	}

        });
        
        form.on('switch(febs-form-group-switch1)', function (data) {
        	if(data.elem.checked) {
        		$searchForm.find('input[name="envoryUpdate1"]').val(1);
        	}else{
        		$searchForm.find('input[name="envoryUpdate1"]').val(0);
        	}
        });

        
        table.on('tool(oilPriceTable)', function (obj) {
            var data = obj.data,
                layEvent = obj.event;
            if (layEvent === 'infoadd') {
            	alert(data.siteid);
            	febs.view.tab.change("/site/shell/info/" + data.siteid);
            }
            if (layEvent === 'delete') {
                febs.modal.confirm('删除用户', '确定删除该用户？', function () {
                	deleteOilPrice(data.id);
                });
            }
        });

        table.on('sort(oilPriceTable)', function (obj) {
            sortObject = obj;
            tableIns.reload({
                initSort: obj,
                where: $.extend(getQueryParams(), {
                    field: obj.field,
                    order: obj.type
                })
            });
        });

        $query.on('click', function () {
            var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            tableIns.reload({where: params, page: {curr: 1}});
        });

        $reset.on('click', function () {
            $searchForm[0].reset();
            $searchForm.find('input[name="oaChecked1"]').val(0);
            $searchForm.find('input[name="envoryUpdate1"]').val(0);
            formSelects.value('oilPrice-search-site', []);
            tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
        });
        
        $add.on('click', function () {
        	febs.modal.open('新增变价', 'price/oilprice/add', {
                btn: ['提交', '重置'],
                area: $(window).width() <= 750 ? '90%' : '50%',
                yes: function (index, layero) {
                    $('#oilPrice-add').find('#submit').trigger('click');
                },
                btn2: function () {
                    $('#oilPrice-add').find('#reset').trigger('click');
                    return false;
                }
            });
        });
        
        $export.on('click', function () {
        	var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
            params.pageSize = $view.find(".layui-laypage-limits option:selected").val();
            params.pageNum = $view.find(".layui-laypage-em").next().html();
            febs.download(ctx + 'user/excel', params, '壳牌油站信息表.xlsx');
        });
        
        $delete.on('click', function () {
        	var checkStatus = table.checkStatus('oilPriceTable');
        	if (!checkStatus.data.length) {
                febs.alert.warn('请选择需要删除的价格变动');
            } else {
                febs.modal.confirm('删除变价信息', '确定删除变价信息？', function () {
                    var Ids = [];
                    layui.each(checkStatus.data, function (key, item) {
                        Ids.push(item.id)
                    });
                    deleteOilPrice(Ids.join(','));
                });
            }
        });

        function initTable() {
            tableIns = febs.table.init({
                elem: $view.find('table'),
                id: 'oilPriceTable',
                url: ctx + 'oilprice/list',
                cols: [[
                    {type: 'checkbox'},
                    {field: 'fromDate', title: '变价日期', minWidth: 130},
                    {field: 'fromTime', title: '变价时间', minWidth: 100},
                    {field: 'oil92', title: '92#', minWidth: 50},
                    {field: 'oil95', title: '95#', minWidth: 50},
                    {field: 'oil0', title: '0#', minWidth: 50},
                    {field: 'siteCode', title: '变价油站', minWidth: 130},
                    {title: 'OA审批', templet: '#oaCheck', minWidth: 80},
                    {title: 'Envory发布', templet: '#envoryUpdate', minWidth: 100},
                    {title: '操作', toolbar: '#oilPrice-option', minWidth: 120}
                ]]
            });
        }

        function getQueryParams() {
        	var fromDateFrom,
        		fromDateTo,
	            fromDate = $searchForm.find('input[name="fromDate"]').val();
	        if (fromDate) {
	        	fromDateFrom = fromDate.split(' - ')[0];
	        	fromDateTo = fromDate.split(' - ')[1];
	        }
            return {
                fromDateFrom: fromDateFrom,
                fromDateTo: fromDateTo,
                siteCode: $searchForm.find('input[name="siteCode1"]').val(),
                priceType: $searchForm.find("select[name='priceType1']").val(),
                oaChecked: $searchForm.find("input[name='oaChecked1']").val(),
                envoryUpdate: $searchForm.find("input[name='envoryUpdate1']").val(),
                invalidate_ie_cache: new Date()
            };
        }

        function deleteOilPrice(Ids) {
            febs.get(ctx + 'oilprice/delete/' + Ids, null, function () {
                febs.alert.success('删除变价成功');
                $query.click();
            });
        }
        
    })
</script>